package com.waimai.app.qa.mail;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;

import com.waimai.app.qa.util.InitProperties;

/**
 * MailInfo Class
 * @author AiHui
 */
public class MailInfo implements IMailInfo {

	// 发送邮件的服务器的IP
	private String ServerHost;
	// 发送邮件的服务器的端口
	private String ServerPort;
	// 邮件发送者的地址
	private String FromAddress;
	// 邮件接收者的地址
	private String ToAddress;
	// 登陆邮件发送服务器的用户名
	private String UserName;
	// 登陆邮件发送服务器的密码
	private String Password;
	// 是否需要身份验证
	private boolean Validate;
	// 邮件主题
	private String Subject;
	// 邮件的文本内容类型
	private String ContentType;
	// 邮件内容
	private String Content;
	// 邮件内容
	private ArrayList<String> Attachment= new ArrayList<String>();
	// 邮件会话属性
	private Properties p = new Properties();

	private final String ENCODING = "UTF-8";

	private final String HTMLPATH = "mail.html.vm";

	private final String TEXTPATH = "mail.text.vm";

	public MailInfo() {
		//从配置文件中获得邮件信息
		ServerHost = System.getProperty("Mail.ServerHost", "smtp.jd.com");
		ServerPort = System.getProperty("Mail.ServerPort", "25");
		FromAddress = System.getProperty("Mail.FromAddress", "");
		ToAddress = System.getProperty("Mail.ToAddress", "");
		UserName = System.getProperty("Mail.UserName", "");
		Password = System.getProperty("Mail.Password", "");
		Validate = System.getProperty("Mail.Validate", "true").equalsIgnoreCase("true");
		Subject = "[" + System.getProperty("Project.Name") + "]" + System.getProperty("Mail.Subject", "自动化邮件主题");
		java.util.Calendar c = java.util.Calendar.getInstance();
		java.text.SimpleDateFormat f = new java.text.SimpleDateFormat("[yyyy年MM月dd日]");
		Subject = f.format(c.getTime()) + Subject;
		ContentType = System.getProperty("Mail.ContentType", "html");
		p.put("mail.smtp.host", ServerHost);
		p.put("mail.smtp.port", ServerPort);
		p.put("mail.smtp.auth", Validate ? "true" : "false");
		Content = formatMailContentFromTemplate();
	}

	public String formatMailContentFromTemplate() {
//		StringBuffer sf = new StringBuffer();
		String line = "";
//		try {
//			BufferedReader reader;
//			if (getContentType().equalsIgnoreCase("html")) {
//				reader = new BufferedReader(new InputStreamReader(this.getClass().getResourceAsStream(HTMLPATH), ENCODING));
//			} else {
//				reader = new BufferedReader(new InputStreamReader(this.getClass().getResourceAsStream(TEXTPATH), ENCODING));
//			}
//			line = reader.readLine();
//			while (line != null) {
//				sf.append(line + "\n");
//				line = reader.readLine();
//			}
//			reader.close();
//			//替换邮件模板中的关键词
//			line = sf.toString();
//			line = line.replaceFirst("#projectname#", String.format("%s",System.getProperty("Project.Name", "测试")));
//			line = line.replaceFirst("#suitenum#", String.format("%d", TestCount.SUITES.size()));
//			line = line.replaceFirst("#testnum#", String.format("%d", TestCount.TESTNUM));
//			line = line.replaceFirst("#testcasenum#", String.format("%d", TestCount.PASSEDNUM+ TestCount.FAILEDNUM + TestCount.SKIPPEDNUM));
//			line = line.replaceFirst("#passnum#", String.format("%d", TestCount.PASSEDNUM));
//			line = line.replaceFirst("#failednum#", String.format("%d", TestCount.FAILEDNUM));
//			line = line.replaceFirst("#skkipednum#", String.format("%d", TestCount.SKIPPEDNUM));
//			line = line.replaceFirst("#starttime#", String.format("%s",(new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date(Long.parseLong(System.getProperty(InitProperties.MILITIMEKEY))))));
//			line = line.replaceFirst("#duringtime#",String.format("%s", getDuringTime()));
//		} catch (IOException ex) {
//			ex.getStackTrace();
//		}
		return line;
	}

	private String getDuringTime() {
		NumberFormat DURATION_FORMAT = new DecimalFormat("#0.000");
		Date end = new Date();
		StringBuilder time = new StringBuilder();
		long diff = end.getTime() - Long.parseLong(System.getProperty(InitProperties.MILITIMEKEY));
		long hour = diff / (60 * 60 * 1000);
		long min = diff / (60 * 1000) - hour * 60;
		String s = DURATION_FORMAT.format(diff / (1000.00) - hour * 60 * 60 - min * 60);
		if (hour > 0) {
			time.append(hour + ":");
		}
		if (hour > 0 || min > 0) {
			time.append(min + ":");
		}
		time.append(s + "s");
		return time.toString();
	}

	@Override
	public String getServerHost() {
		return ServerHost;
	}

	public void setServerHost(String serverHost) {
		this.ServerHost=serverHost;
		p.put("mail.smtp.host", serverHost);
	}
	
	@Override
	public String getServerPort() {
		return ServerPort;
	}

	public void setServerPort(String serverPort) {
		this.ServerPort=serverPort;
		p.put("mail.smtp.port", serverPort);
	}
	
	@Override
	public String getFromAddress() {
		return FromAddress;
	}

	public void setFromAddress(String fromAddress) {
		this.FromAddress=fromAddress;
	}
	
	@Override
	public String getToAddress() {
		return ToAddress;
	}

	public void setToAddress(String toAddress) {
		this.ToAddress=toAddress;
	}
	
	@Override
	public String getUserName() {
		return UserName;
	}

	public void setUserName(String userName) {
		this.UserName=userName;
	}
	
	@Override
	public String getPassword() {
		return Password;
	}

	public void setPassword(String password) {
		this.Password=password;
	}
	
	@Override
	public Boolean getValidate() {
		return Validate;
	}

	public void setValidate(boolean validate) {
		this.Validate=validate;
		p.put("mail.smtp.auth", validate ? "true" : "false");
	}
	
	@Override
	public String getSubject() {
		return Subject;
	}

	public void setSubject(String subject) {
		this.Subject=subject;
	}
	
	@Override
	public String getContent() {
		return Content;
	}

	public void setContent(String content) {
		this.Content=content;
	}
	
	@Override
	public String getContentType() {
		return ContentType;
	}

	public void setContentType(String contentType) {
		this.ContentType=contentType;
	}
	
	@Override
	public Properties getProperties() {
		return p;
	}

	@Override
	public List<String> getAttachment() {
		// TODO Auto-generated method stub
		return Attachment;
	}
	
	public void addAttachment(String attachmentPath ) {
		// TODO Auto-generated method stub
		Attachment.add(attachmentPath);
	}

	@Override
	public void clearAttachment() {
		// TODO Auto-generated method stub
		Attachment.clear();
	}
}
